class Controller {
    constructor (systemService, uiNotification, statusMap, $stateParams, $modalInstance, node) {
        "ngInject"
        this.service      = systemService;
        this.notify       = uiNotification;
        this.uiModal      = $modalInstance;
        this.status       = statusMap;
        this.type         = node.type;
        this.classify     = node.classify;
        this.activeId     = 'org';
        this.orgId        = $stateParams.id;
        this.checkedItem  = node.context;
        this.checkId      = node.checkId;//选中的岗位或员工id

        this.page = systemService.page;

        __DEBUG__ && console.log(this.orgId)

        angular.extend(this.page, {
            currentPage: $stateParams.page ? $stateParams.page : 1
        });

        this.tempType     = this.type.replace(this.type.charAt(0), (s) => {
            return s.toUpperCase()
        })
        this.tempClassify = this.classify.replace(this.classify.charAt(0), (s) => {
            return s.toUpperCase()
        })
        this.classifyType = this.tempClassify + this.tempType;

        this[ this.classify + this.tempType + 'Specify' ]  = [];
        this[ this.classify + this.tempType + 'Specify2' ] = [];

        if ( this.type == 'role' ) this.loadRole(this.classify)

    }

    loadPost (type) {
        if ( this[ this.classify + this.tempType + 'Specify' ].length ) this[ this.classify + this.tempType + 'Specify' ] = [];
        let param = (type == 'org' ? {orgId: this.orgId} : {rzOrgId: this.orgId})
        angular.extend(param, {statusCd: 'S0A',sysUserId : this.checkedItem['sysUserId']})
        this.service
            .getList('post', param)
            .then((res) => {
                type == 'org' ? this.isOrgLoad = true : this.isRzOrgLoad = true;
                this.postList = res.data.list;
                angular.forEach(this.postList, (data) => {
                    if ( data[ 'sys' + this.classifyType + 'Id' ] ) {
                        this[ this.classify + this.tempType + 'Specify' ].push(data);
                        this[ this.classify + this.tempType + 'Specify2' ].push(data);
                        data.checked = true;
                    }
                });
            }).catch((data) => {
            this.notify.error({
                'title'  : '提示',
                'message': '查询失败. ' + (data.resultMsg ? data.resultMsg : '')
            });
        })
    }

    loadRole (type) {
        let param = {};
        if(this.classify == 'post'){
            angular.extend(param, {sysPostId:this.checkedItem['sysPostId']})
        }else if(this.classify == 'user'){
            angular.extend(param, {sysUserId:this.checkedItem['sysUserId']})
        }
        this.service[type + '2Roles'](param, this.page)
            .then((res) => {
                this.isRoleLoad = true;
                this.roleList   = res.data.list;
                angular.forEach(this.roleList, (data) => {
                    if ( data[ 'sys' + this.classifyType + 'Id' ] ) {
                        this[ this.classify + this.tempType + 'Specify' ].push(data);
                        this[ this.classify + this.tempType + 'Specify2' ].push(data);
                        data.checked = true;
                    }
                });

            }).catch((data) => {
            this.notify.error({
                'title'  : '提示',
                'message': '查询失败. ' + (data.resultMsg ? data.resultMsg : '')
            });
        })
    }

    toggleCheck (item) {
        __DEBUG__ && console.log(item)
        item.checked = !item.checked;
        let idx      = _.indexOf(this[ this.classify + this.tempType + 'Specify' ][ 'sys' + this.type + 'Id' ], item[ 'sys' + this.tempType + 'Id' ]);
        if ( item.checked ) {
            if ( idx === -1 ) {
                this[ this.classify + this.tempType + 'Specify2' ].push(item)
            }
        } else {
            this[ this.classify + this.tempType + 'Specify2' ].splice(idx, 1);

        }
    }

    save () {
        //遍历出所有选中input checkbox
        let paramAdd = {};
        let paramDel = {};
        if ( this.submitting ) return;
        this.submitting = true;

        //对比已有的角色数组跟重新勾选的角色数组
        let delList = _.difference(this[ this.classify + this.tempType + 'Specify' ], this[ this.classify + this.tempType + 'Specify2' ]);
        let addList = _.difference(this[ this.classify + this.tempType + 'Specify2' ], this[ this.classify + this.tempType + 'Specify' ]);
        let delIds  = [];
        let addIds  = [];

        angular.forEach(delList, (data) => {
            delIds.push(data[ 'sys' + this.classifyType + 'Id' ])
        });
        angular.forEach(addList, (data) => {
            addIds.push(data[ 'sys' + this.tempType + 'Id' ])
        });


        paramAdd[ 'sys' + this.tempClassify + 'Id' ] = this.checkedItem[ 'sys' + this.tempClassify + 'Id' ];
        paramAdd[ 'ids' ]                            = addIds.join(',');
        paramDel                                     = {
            ids: delIds.join(',')
        }

        // 指定岗位需带org_id
        if ( this.type == 'post' ) {
            angular.extend(paramAdd, {
                orgId: this.orgId
            })
        }

        if ( addList.length == 0 && delList.length == 0 ) {
            this.notify.error({
                'title'  : '提示',
                'message': '请选择!'
            });
        }
        // 新增
        if ( addList.length != 0 ) {
            this.service[ this.classify + this.tempType + "Insert" ](paramAdd).then(() => {
                this.uiModal.close()
                this.notify.success({
                    'title'  : '提示',
                    'message': '新增成功!'
                });
            }).catch((data) => {
              this.notify.error({
                  'title'  : '提示',
                  'message': '新增失败. ' + (data.resultMsg ? data.resultMsg : '')
              });
            }).finally(() => {
                // 隐藏 loading
                this.submitting = false;
            });
        }


        // 删除
        if ( delList.length != 0 ) {
            this.service[ this.classify + this.tempType + "Delete" ](paramDel).then(() => {
                this.uiModal.close();
                this.notify.success({
                    'title'  : '提示',
                    'message': '删除成功!'
                });
            }).catch((data) => {
                this.notify.error({
                    'title'  : '提示',
                    'message': '删除失败. ' + (data.resultMsg ? data.resultMsg : '')
                });
            }).finally(() => {
                // 隐藏 loading
                this.submitting = false;
            });
        }

    }

    cancel () {
        this.uiModal.dismiss('cancel')
    }
}

export default Controller;